package com.haofengsoft.scce.ui.consultation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.platform.utils.NetWorkUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.FocusNewModel;
import com.haofengsoft.scce.adapter.information.NanJingAdapter;
import com.haofengsoft.scce.adapter.information.NanJingModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.consultation.NanJingController;
import com.haofengsoft.scce.ui.fragment.BaseFragment;

public class StampNanJingFragment extends BaseFragment {

    private PullToRefreshListView nanjingPtrLv;
    private NanJingAdapter nanJingAdapter = null;

    private int pageIndex = 0;


    private Context context;


    public static StampNanJingFragment newInstance(String key) {
        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        StampNanJingFragment fragment = new StampNanJingFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveStateToArguments(outState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        saveStateToArguments(getArguments());
        // 如果onSaveInstanceState没被调用，这里也可以保存数据
    }

    public void saveStateToArguments(Bundle outState) {
        if (nanJingAdapter != null) {
            List<NanJingModel> data = nanJingAdapter.getNanJingModelList();
            ArrayList<NanJingModel> modelArrayList = new ArrayList<NanJingModel>();
            modelArrayList.addAll(data);
            outState.putSerializable("data", modelArrayList);
            outState.putInt("page", pageIndex);
        }
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.consultation_stamp_nanjing, container, false);
        view.setOnTouchListener(new DontSpillOnTouchListener());
        context = getActivity();
        view.setOnTouchListener(new DontSpillOnTouchListener());
        nanjingPtrLv = (PullToRefreshListView) view.findViewById(R.id.consultation_nanjing_ptrl);
        nanjingPtrLv.setMode(PullToRefreshBase.Mode.BOTH);
        ImageView nodataIv = (ImageView) view.findViewById(R.id.nanjing_no_data_iv);
        nodataIv.setOnClickListener(new DataLoadOnClickListener());
        nanjingPtrLv.setOnFocusChangeListener(null);
        nanjingPtrLv.setOnHierarchyChangeListener(null);
        nanjingPtrLv.setOnRefreshListener(new LoadPullData());
        nanjingPtrLv.setEmptyView(nodataIv);
        nanjingPtrLv.setScrollEmptyView(true);
        if (savedInstanceState != null && (savedInstanceState
                .getSerializable("data") != null)) {
            ArrayList<NanJingModel> data = (ArrayList<NanJingModel>) savedInstanceState
                    .getSerializable("data");
            pageIndex = savedInstanceState.getInt("page");
            nanJingAdapter = new NanJingAdapter(context, data);
            nanjingPtrLv.setAdapter(nanJingAdapter);
        } else {
            initVolleyData();
        }
        return view;
    }

    class DataLoadOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initVolleyData();
        }
    }

    class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
        // 下拉(手势下拉)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            pageIndex = 0;
            nanJingAdapter = null;
            initVolleyData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            initVolleyData();
        }
    }

    private void refreshList(List<NanJingModel> modelList) {
        if (null != modelList) {
            if (null == nanJingAdapter) {
                nanJingAdapter = new NanJingAdapter(context, modelList);
                nanjingPtrLv.setAdapter(nanJingAdapter);
            } else {
                nanJingAdapter.addNanJingList(modelList);
            }
            pageIndex = pageIndex + 1;
        } else {
            Toast.makeText(getActivity(), "没有信息了", Toast.LENGTH_SHORT).show();
        }
    }

    private void initVolleyData() {
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("PageIndex", pageIndex);
        mapParam.put("PageSize", 10);
        mapParam.put("TypeID", 1);

        VolleyManage.getInstance().methodPost(getActivity(), null, ConstantConfig.TYPE_NEWS_URL, mapParam,
                new NanJingVolleyReal());
    }

    @Override
    protected void lazyLoad() {
//        if (!isVisible) {
//            //不显示
//            return;
//        }
//        if (!NetWorkUtil.isNetworkConnected(context)) {
//            //已经初始化，且已经获取数据
//            //从本地找
//            List<NanJingModel> modelList = NanJingController.getInstance(context).gainNanJingModels();
//            nanJingAdapter = null;
//            pageIndex = 0;
//            refreshList(modelList);
//        } else {
//            initVolleyData();
//        }
    }

    class NanJingVolleyReal implements VolleyInterface {
        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                List<NanJingModel> modelList = NanJingController.getInstance(getActivity()).handleNanJing(bean.getContent());
                refreshList(modelList);

            } else {
                Toast.makeText(getActivity(), "网络连接错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
